<template>
  <el-dialog :title="info.title" :visible.sync="visible" center width="960px" :before-close="handleCancel">
    <div class="">
      <div class="page-filter mb10 fl">
        <el-form ref="filter" :model="filter" label-width="0">
          <el-form-item class="mr10 mb5 fl">
            <el-input v-model="filter.name" placeholder="证书名称" size="small" clearable />
          </el-form-item>
          <el-form-item class="mr10 mb5 fl" style="width: 300px">
            <el-button class="fl" type="primary" size="small" @click="getList">查 询</el-button>
            <el-button class="fl" type="primary" size="small" @click="handleAdd">新 增</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table ref="multipleTable" class="search-table" :data="list" border fit highlight-current-row style="width: 100%;">
        <el-table-column label="序号" align="center" width="56px">
          <template slot-scope="{$index}">
            {{ $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="证书名称" width="220px" prop="name" />
        <el-table-column label="起始日期" align="center" width="110px" prop="begin_time" />
        <el-table-column label="结束日期" align="center" width="110px" prop="end_time" />
        <el-table-column label="备注" min-width="220px" prop="remark" />
        <el-table-column label="操作" align="center" width="110px" fixed="right">
          <template slot-scope="{row}">
            <el-dropdown style="height:29px">
              <el-button size="mini" type="primary">
                操作<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleEdit(row)">编 辑</el-dropdown-item>
                <el-dropdown-item @click.native="archives(row)">附 件</el-dropdown-item>
                <el-dropdown-item @click.native="handleDelete(row)">删 除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="filter.pageAll>0" :total="filter.pageAll" :page.sync="filter.pageCurrent" :limit.sync="filter.pageNumber" @pagination="getList" />
      <Dialog ref="dialog" :get-data="getList" :info="dialog" />
      <Archives ref="archives" :get-data="getList" :info="dialog" />
    </div>
  </el-dialog>
</template>

<script>
import { delCredentials, credentials } from '@/api/staff'
import Pagination from '@/components/Pagination'
import Dialog from './components/credential-dialog'
//import Archives from './components/archives-dialog'

export default {
  components: { Pagination, Dialog },
  inject: ['app'],
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      form: {},
      filter: {
        pageNumber: 10,
        pageCurrent: 1,
        pageAll: 1
      },
      dialog: {
        edit: {},
        title: '',
        type: ''
      },
      list: [{name: '名称'}],
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.$set(this, 'form', this.info.edit)
        this.getList()
        this.$nextTick(() => {
          this.$refs.multipleTable.doLayout()
        })
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    getList(e) {
      credentials({
        ...this.filter,
        ...e,
        ...{'su_id': this.form.id}
      }).then(response => {
        this.list = response.list
        this.filter = response.filter
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    handleDelete(e) {
      this.$confirm('此操作将删除数据信息，数据将无法恢复, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        delCredentials({
          ids: e['id']
        }).then(response => {
          this.getList()
          this.$message.success('删除成功')
        })
      })
    },
    archives(e) {
      this.dialog.title = '证书附件'
      this.app.dialogParam(this.dialog.edit, ['id'], e)
      this.$refs['archives'].visible = true
    },
    handleEdit(e) {
      this.dialog.title = '编辑证书'
      this.dialog.type = 'edit'
      e.su_id = this.form.id
      this.app.dialogParam(this.dialog.edit, ['id', 'name', 'su_id', 'begin_time', 'end_time', 'remark'], e)
      this.$refs['dialog'].visible = true
    },
    handleAdd(e) {
      this.dialog.title = '新增证书'
      this.dialog.type = 'add'
      e.su_id = this.form.id
      this.app.dialogParam(this.dialog.edit, ['id', 'name', 'su_id', 'begin_time', 'end_time', 'remark'], e)
      this.$refs['dialog'].visible = true
    },
    handleCancel(done) {
      // this.getData()
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style scoped>
</style>
